package com.yigang.service.impl;

import java.util.List;
import java.util.Map;

import com.yigang.entity.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yigang.dao.ProductMapper;
import com.yigang.entity.ImgAndProduct;
import com.yigang.entity.OrderAndProduct;
import com.yigang.entity.Product;
import com.yigang.service.ProductService;
import com.yigang.utils.ResultUtil;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductMapper productMapper;

	public ProductMapper getProductMapper() {
		return productMapper;
	}

	public void setProductMapper(ProductMapper productMapper) {
		this.productMapper = productMapper;
	}

	@Override
	public ResultUtil selectProductList(Integer page, Integer limit, Product record) {
		PageHelper.startPage(page, limit);
		List<Product> products = productMapper.selectProductList(record);
		PageInfo<Product> pageInfo = new PageInfo<Product>(products);
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}

	@Override
	public ResultUtil deleteByPrimaryKey(Integer productId) {
		// TODO Auto-generated method stub
		productMapper.deleteByPrimaryKey(productId);
		return ResultUtil.ok();
	}

	@Override
	public ResultUtil insertSelective(Product record) {
		// TODO Auto-generated method stub
		productMapper.insertSelective(record);
		return ResultUtil.ok();
	}

	@Override
	public ResultUtil updateByPrimaryKeySelective(Product record) {
		// TODO Auto-generated method stub
		productMapper.updateByPrimaryKeySelective(record);
		return ResultUtil.ok();
	}

	@Override
	public Product selectProductById(int id) {
		// TODO Auto-generated method stub
		return productMapper.selectProductById(id);
	}

	@Override
	public ResultUtil updateProductStatusById(int productId, int productRecommend) {
		// TODO Auto-generated method stub
		productMapper.updateProductStatusById(productId, productRecommend);
		return ResultUtil.ok();
	}

	@Override
	public ResultUtil updateProductShelfById(int productId, int productShelf) {
		// TODO Auto-generated method stub
		productMapper.updateProductShelfById(productId, productShelf);
		return ResultUtil.ok();
	}

	@Override
	public List<Map<String, Object>> getAllproduct(String searchKey,Integer classification_id) {
		// TODO Auto-generated method stub
		return productMapper.getAllproduct(searchKey,classification_id);
	}



	@Override
	public List<Map<String, Object>> productRecommend() {
		// TODO Auto-generated method stub
		return productMapper.productRecommend();
	}

	@Override
	public List<Map<String, Object>> productRecommend1() {
		// TODO Auto-generated method stub
		return productMapper.productRecommend1();
	}

	@Override
	public List<Map<String, Object>> productRecommend2() {
		// TODO Auto-generated method stub
		return productMapper.productRecommend2();
	}

	/*
	 * @Override public List<Map<String, Object>> getProductList() { // TODO
	 * Auto-generated method stub return productMapper.getProductList(); }
	 */

	@Override
	public List<Map<String, Object>> getAllpage() {
		// TODO Auto-generated method stub
		return productMapper.getAllpage();
	}

	@Override
	public List<Map<String, Object>> getAllChanel() {
		// TODO Auto-generated method stub
		return productMapper.getAllChanel();
	}

	@Override
	public List<Map<String, Object>> getProductList(int product_id) {
		// TODO Auto-generated method stub
		return productMapper.getProductList(product_id);
	}
	@Override
	public Product getById(int productId) {
		// TODO Auto-generated method stub
		return productMapper.getById(productId);
	}
	@Override
	public ProductVO getColorById(int productId) {
		// TODO Auto-generated method stub
		return productMapper.getColorById(productId);
	}

	@Override
	public Product getAllproductById(Integer classification_id) {
		// TODO Auto-generated method stub
		return productMapper.getAllproductById(classification_id);
	}

	@Override
	public ResultUtil getProductImg(Integer page, Integer limit, ImgAndProduct imgAndProduct) {
		PageHelper.startPage(page, limit);
		List<ImgAndProduct> productimg = productMapper.selectImgAndProduct(imgAndProduct);
		PageInfo<ImgAndProduct> pageInfo = new PageInfo<ImgAndProduct>(productimg);
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}

	@Override
	public ImgAndProduct selectImgAndProduct(ImgAndProduct imgAndProduct) {
		List<ImgAndProduct> productimg = productMapper.selectImgAndProduct(imgAndProduct);
		ImgAndProduct res = new ImgAndProduct();
		res.setProduct_img(productimg.get(0).getProduct_img());
		res.setProduct_introductionimg(productimg.get(0).getProduct_introductionimg());
		res.setProduct_name(productimg.get(0).getProduct_name());
		res.setProductimg_id(productimg.get(0).getProductimg_id());
		res.setProduct_id(productimg.get(0).getProduct_id());
		return res;
	}

}
